/*
 * Created by JFormDesigner on Mon Jun 12 14:10:39 CST 2023
 */

package fm.trade;

import dao.Dao_trade;
import model.Trade;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class Update extends JInternalFrame {

    private String [] columnNames = {"商品编号", "商品名称", "商品类别", "数量", "折扣", "单价"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columnNames.length];

        for(int i = 0; i < list.size(); i++)
        {
            Trade trade = (Trade) list.get(i);

            results[i][0] = trade.getTradeID();
            results[i][1] = trade.getTradeName();
            results[i][2] = Dao_trade.getTradeName_ID( trade.getTypeID());
            results[i][3] = trade.getNumber();
            results[i][4] = trade.getDiscount();
            results[i][5] = trade.getPrice();
        }
        return results;
    }

    public Update() {
        initComponents();
        iniData();
    }

    public void iniData()
    {
        Object [][] results = getFS(Dao_trade.fuzzySelectAll());
        DefaultTableModel model = new DefaultTableModel(results, columnNames);
        table.setModel(model);

        Vector<String> its = Dao_trade.getTradeType();
        DefaultComboBoxModel<String> model_1 = new DefaultComboBoxModel<>(its);
        typeComboBox.setModel(model_1);
    }
    private void Cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void tableMouseClicked(MouseEvent e) {
        // TODO add your code here
        int setRow = table.getSelectedRow();
        if(setRow == -1) return;

        String tradeID = table.getValueAt(setRow,0).toString().trim();
        String tradeName = table.getValueAt(setRow,1).toString().trim();
        String tradeType = table.getValueAt(setRow,2).toString().trim();
        String tradeDiscount = table.getValueAt(setRow,4).toString().trim();
        String tradePrice = table.getValueAt(setRow,5).toString().trim();
        String tradeNumber = table.getValueAt(setRow,3).toString().trim();

        tradeIDTextField.setText(tradeID);
        tradeNameTextField.setText(tradeName);
        typeComboBox.setSelectedItem(tradeType);
        tradeDiscountTextField.setText(tradeDiscount);
        tradePriceTtextField.setText(tradePrice);
        tradeNumberTextField.setText(tradeNumber);
    }

    private void Delete(ActionEvent e) {
        // TODO add your code here
        String tradeID = tradeIDTextField.getText();
        int i = Dao_trade.deleteTrade(tradeID);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"删除成功");

            Object [][] results = getFS(Dao_trade.fuzzySelectAll());
            DefaultTableModel tableModel = new DefaultTableModel(results,columnNames);
            table.setModel(tableModel);

            doDefaultCloseAction();
        }
    }

    private void Update(ActionEvent e) {
        // TODO add your code here
        if(tradeNameTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品名称");
            return;
        }

        if(tradePriceTtextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入单价！");
            return;
        }

        if(tradeNumberTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品数量！");
            return;
        }

        if(tradeDiscountTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入折扣！");
            return;
        }

        Double rap = Double.valueOf(tradeDiscountTextField.getText().trim());
        if(rap > 1 || rap ==0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的折扣！");
            return;
        }

        Double yc = Double.valueOf(tradeNumberTextField.getText().trim());
        if (yc <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的数量！");
            return;
        }

        Double ycy = Double.valueOf(tradePriceTtextField.getText().trim());
        if(ycy <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的单价！");
            return;
        }

        String tradeID = tradeIDTextField.getText().trim();

        String tradeType = (String) typeComboBox.getSelectedItem();
        String typeID = Dao_trade.getTypeID_Name(tradeType);

        String tradeNumber = tradeNumberTextField.getText().trim();
        String tradePrice = tradePriceTtextField.getText().trim();
        String tradeDiscount = tradeDiscountTextField.getText().trim();
        String tradeName = tradeNameTextField.getText().trim();

        int i = Dao_trade.update(tradeID,typeID,tradeName,tradeNumber,Double.valueOf(tradePrice),Double.valueOf(tradeDiscount));
        if( i == 1)
        {
            JOptionPane.showMessageDialog(null,"修改成功！");

            Object [][] results = getFS(Dao_trade.fuzzySelectAll());
            DefaultTableModel tableModel = new DefaultTableModel(results,columnNames);
            table.setModel(tableModel);

            doDefaultCloseAction();
        }

    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table = new JTable();
        panel2 = new JPanel();
        UpdateButton = new JButton();
        CancelButton = new JButton();
        DeleteButton = new JButton();
        panel3 = new JPanel();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        tradeIDTextField = new JTextField();
        tradeDiscountTextField = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        tradePriceTtextField = new JTextField();
        tradeNameTextField = new JTextField();
        tradeNumberTextField = new JTextField();
        typeComboBox = new JComboBox<>();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u4fee\u6539\u5546\u54c1");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(0, 200));
            panel1.setBorder(new TitledBorder("\u9009\u62e9\u5546\u54c1"));
            panel1.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table ----
                table.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        tableMouseClicked(e);
                    }
                });
                scrollPane1.setViewportView(table);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(10, 20, 540, 170);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 558, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(372, 40));
            panel2.setLayout(null);

            //---- UpdateButton ----
            UpdateButton.setText("\u786e\u5b9a");
            UpdateButton.addActionListener(e -> Update(e));
            panel2.add(UpdateButton);
            UpdateButton.setBounds(new Rectangle(new Point(177, 5), UpdateButton.getPreferredSize()));

            //---- CancelButton ----
            CancelButton.setText("\u53d6\u6d88");
            CancelButton.addActionListener(e -> Cancel(e));
            panel2.add(CancelButton);
            CancelButton.setBounds(new Rectangle(new Point(325, 5), CancelButton.getPreferredSize()));

            //---- DeleteButton ----
            DeleteButton.setText("\u5220\u9664");
            DeleteButton.addActionListener(e -> Delete(e));
            panel2.add(DeleteButton);
            DeleteButton.setBounds(new Rectangle(new Point(250, 5), DeleteButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 344, 558, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setForeground(Color.black);
            panel3.setBorder(new TitledBorder("\u5546\u54c1\u4fe1\u606f\u4fee\u6539"));
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u5546\u54c1\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(55, 21, 60, 24);

            //---- label2 ----
            label2.setText("\u5546\u54c1\u540d\u79f0\uff1a");
            panel3.add(label2);
            label2.setBounds(282, 20, 80, 24);

            //---- label3 ----
            label3.setText("\u5546\u54c1\u7c7b\u578b\uff1a");
            panel3.add(label3);
            label3.setBounds(55, 61, 60, 24);

            //---- label4 ----
            label4.setText("\u5546\u54c1\u6298\u6263\uff1a");
            panel3.add(label4);
            label4.setBounds(282, 61, 60, 24);

            //---- label5 ----
            label5.setText("\u5546\u54c1\u4ef7\u683c\uff1a");
            panel3.add(label5);
            label5.setBounds(56, 101, 60, 24);

            //---- label6 ----
            label6.setText("\u5546\u54c1\u6570\u91cf\uff1a");
            panel3.add(label6);
            label6.setBounds(282, 101, 60, 24);

            //---- tradeIDTextField ----
            tradeIDTextField.setEditable(false);
            tradeIDTextField.setEnabled(false);
            panel3.add(tradeIDTextField);
            tradeIDTextField.setBounds(120, 19, 136, tradeIDTextField.getPreferredSize().height);
            panel3.add(tradeDiscountTextField);
            tradeDiscountTextField.setBounds(343, 60, 135, 28);
            panel3.add(textField3);
            textField3.setBounds(0, 163, 135, 28);
            panel3.add(textField4);
            textField4.setBounds(0, 196, 135, 28);
            panel3.add(tradePriceTtextField);
            tradePriceTtextField.setBounds(120, 100, 137, tradePriceTtextField.getPreferredSize().height);
            panel3.add(tradeNameTextField);
            tradeNameTextField.setBounds(343, 19, 135, 28);
            panel3.add(tradeNumberTextField);
            tradeNumberTextField.setBounds(343, 100, 135, 28);
            panel3.add(typeComboBox);
            typeComboBox.setBounds(120, 59, 135, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 200, 558, 144);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table;
    private JPanel panel2;
    private JButton UpdateButton;
    private JButton CancelButton;
    private JButton DeleteButton;
    private JPanel panel3;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JTextField tradeIDTextField;
    private JTextField tradeDiscountTextField;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField tradePriceTtextField;
    private JTextField tradeNameTextField;
    private JTextField tradeNumberTextField;
    private JComboBox<String> typeComboBox;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
